/*************************************************
 * File:    		SMView.h
 * Author:  	HouJiong <hou_jiong@163.com>
 * Brief:   		StoneMoneky project.
 *
 * Copyright (c) HouJiong
 *
 *************************************************/

/*
 * History:
 * =======================================
 * 2009-04-10 HouJiong <hou_jiong@163.com> created
 */
 
#include "SMView.h"
#include "SMtypedef.h"

#include <qpushbutton.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qpixmap.h>
#include <qtimer.h>



SMView::SMView(QWidget *parent,const char* name)
:QWidget(parent,name)
{
	m_SMJS=NULL;
	m_SMXMLDOM=NULL;
	resize(240,320);
}

void SMView::mousePressEvent( QMouseEvent *e )
{

	ElementList::iterator nodeIterator;  
   
	for(nodeIterator = m_SMXMLDOM->getElementList().begin(); nodeIterator !=  m_SMXMLDOM->getElementList().end(); nodeIterator++)   
	{  
			DEBUG_LINE();
		if(atoi(((*nodeIterator).getAttribute("x")).c_str())<e->x()&&
				(atoi(((*nodeIterator).getAttribute("x")).c_str())+atoi(((*nodeIterator).getAttribute("width")).c_str()))>e->x()&&
				atoi(((*nodeIterator).getAttribute("y")).c_str())<e->y()&&
				(atoi(((*nodeIterator).getAttribute("y")).c_str())+atoi(((*nodeIterator).getAttribute("height")).c_str()))>e->y())
		{
	
			if(((*nodeIterator).getAttribute("onClicked"))!="NULL")
				{
						m_SMJS->callback( (*nodeIterator).getAttribute("onClicked"));
				}
		}
	}
}


void SMView::paintEvent( QPaintEvent *e )
{

    QRect cr(0, 0, 1000, 1000);
    QPixmap pix( cr.size() );
    pix.fill( this, cr.topLeft() );
	
    QPainter p( &pix );	
	

	ElementList::iterator elementIterator;  
   	for(elementIterator = m_SMXMLDOM->getElementList().begin(); elementIterator !=  m_SMXMLDOM->getElementList().end(); elementIterator++)   
	{  
		if((*elementIterator).getAttribute("tag")=="view")
		{
			QPixmap pixmap((*elementIterator).getAttribute("src"));
			p.drawPixmap(atoi((*elementIterator).getAttribute("x").c_str()),
									atoi((*elementIterator).getAttribute("y").c_str()),
									pixmap);							
		}	

		if((*elementIterator).getAttribute("tag")=="img")
		{
			QPixmap pixmap((*elementIterator).getAttribute("src"));
			p.drawPixmap(atoi((*elementIterator).getAttribute("x").c_str()),
									atoi((*elementIterator).getAttribute("y").c_str()),
									pixmap);							
		}	
				
	}

    p.end();
    p.begin( this );
    p.drawPixmap( cr.topLeft(), pix );

}


bool SMView::loadXML(SMXMLDOM *xmldom)
{
	if(!xmldom)
	{
		DEBUG_ERROR();
		return false;
	}	
	m_SMXMLDOM=xmldom;
}

bool SMView::loadJS(SMJS *js)
{
	if(!js)
	{
		DEBUG_ERROR();
		return false;
	}
	m_SMJS=js;
}


